//
//  ReplaceDetailTableViewController.m
//  JxCarios
//
//  Created by xcq on 15/1/29.
//  Copyright (c) 2015年 xiongchuanqi. All rights reserved.
//

#import "ReplaceDetailTableViewController.h"
//#import "ReplaceDriveShopPhoneCell.h"
#import "ReplaceDriveDetailPriceCell.h"
#import "ReplaceDriveDetailCallCell.h"
#import "RoadsideAssistanceShopServiceCell.h"
#import "SettlemanetView.h"
#import "PayViewController.h"

#define sectionFooterH 80
static NSString *cellStr_one = @"replaceDrive";
static NSString *cellStr_two = @"detailprice";
static NSString *cellStr_three = @"detailcall";
//static NSString *cellSte_two_pay = @"RoadsideAssistanceShopServiceCell";


@interface ReplaceDetailTableViewController () <checkActionDelegate>
{
    SettlemanetView *settleView;
    NSMutableArray *seleteShop;
    double _checkInteger;
}
@end

@implementation ReplaceDetailTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initAbout];
    _checkInteger = 0;
    self.tableView.hidden = YES;
    _carAround = [CarAroundDataSource dataSourceWithRequestor:self];
    _carAround.shopId = _sellerid;
    _carAround.dic_type = @"travel";
    [_carAround getShopTravelAndPullcar:^(id result) {
        _resultDic = result;
        [self initDataSource:(NSDictionary*)result];
        NSLog(@"%@",result);
    }anfFail:^(id result) {
         
    }];
}

- (void)initDataSource:(NSDictionary *)result{
    
    NSDictionary *dic = [result objectForKey:@"seller"];
//    if ([[dic customObjectForkey:@"IS_PAY"] isEqualToString:@"yes"]) {
//        _is_pay = YES;
//    }else{
//        _is_pay = NO;
//    }
    if([dic objectForKey:@"TEL"] != [NSNull null] && [dic objectForKey:@"TEL"] != nil){
        NSArray *array = [[dic objectForKey:@"TEL"] componentsSeparatedByString:@","];
        _phoneArray_two = [NSMutableArray arrayWithArray:array];
        int i = 0 ;
        BOOL replace = NO;
        for (int a = 0 ;a < _phoneArray_two.count; a ++) {
            NSString *str = [_phoneArray_two objectAtIndex:a];
            if ([str isEqualToString:@"null"]) {
                i = a;
                replace = YES;
            }
        }
        if (replace) {
            [_phoneArray_two removeObjectAtIndex:i];
        }
    }
    
    _phoneArray_one = [NSMutableArray arrayWithArray:[result objectForKey:@"serviceone"]];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    self.tableView.hidden = NO;
    [self.tableView reloadData];

}

- (void)initAbout{
    self.title = @"代驾";
    [self showNavBarWithRightTitle:nil andRightAction:nil andLeftAction:nil andCustom:NO];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 30, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStyleGrouped];
    
    self.tableView.tableFooterView = [UIView new];
    [self.view addSubview:self.tableView];
    REGISTCELL(@"ReplaceDriveTableViewCell", cellStr_one);
    REGISTCELL(@"ReplaceDriveDetailPriceCell", cellStr_two);
    REGISTCELL(@"ReplaceDriveDetailCallCell", cellStr_three);
//    REGISTCELL(@"RoadsideAssistanceShopServiceCell", cellSte_two_pay);
    
    settleView = [[[NSBundle mainBundle] loadNibNamed:@"SettlemanetView" owner:self options:nil] lastObject];
    settleView.frame = CGRectMake(0,[RextExtends getScreenFrame].size.height - 62 , [RextExtends getScreenFrame].size.width, 62);
    settleView.hidden = YES;
    settleView.tager = self;
    settleView.action = @selector(pushPayViewController);
    [self.view addSubview:settleView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    NSLog(@"%s",__FUNCTION__);

    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    switch (section) {
        case 0 :
            return 1;
            break;
        case 1:
            NSLog(@"%s",__FUNCTION__);

            return [_phoneArray_one count] <=0 ? 0 : [_phoneArray_one count];
            break;
        case 2:
            return _phoneArray_two.count <=0 ? 0 : _phoneArray_two.count;

            break;
      
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:{
           ReplaceDriveTableViewCell* cell = [tableView dequeueReusableCellWithIdentifier:cellStr_one forIndexPath:indexPath];
            cell.info = [_resultDic customObjectForkey:@"seller"];
            CGFloat distance = [_distance doubleValue] / 1000;
            cell.djDistance.text = [[NSString stringWithFormat:@"%.1f",distance] stringByAppendingString:@"km"];
             //TODO:赋值
            return cell;
            break;
        }
        case 1:{
            NSString *str = @"￥";
 
                ReplaceDriveDetailPriceCell *price = [tableView dequeueReusableCellWithIdentifier:cellStr_two forIndexPath:indexPath];
                NSDictionary *dic = [_phoneArray_one objectAtIndex:indexPath.row];
                price.timeLabel.text = [dic customObjectForkey:@"GOOD_NAME"];
                price.priceLabel.text = [str stringByAppendingString:[dic customObjectForkey:@"CHEAP_PRICE"]];
                price.linePrice.text = [str stringByAppendingString:[dic customObjectForkey:@"PRICE"]];
                return price;
          
            break;
        }
        case 2:{
            ReplaceDriveDetailCallCell *phone = [tableView dequeueReusableCellWithIdentifier:cellStr_three forIndexPath:indexPath];
            phone.phone.text = [_phoneArray_two objectAtIndex:indexPath.row];
            phone.count = indexPath.row;
            return phone;
            break;
        }
        default:
            break;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
  //  RoadsideAssistanceShopServiceCell *_cell = (RoadsideAssistanceShopServiceCell*)[tableView cellForRowAtIndexPath:indexPath];
  //  [_cell checkAction:_cell.checkBtn];
}


////点击回调
//- (void)checkCallBack:(UITableViewCell *)cell andSeleted:(BOOL)seleted{
//    if (!seleteShop) {
//        seleteShop = [NSMutableArray new];
//    }
//    
//    RoadsideAssistanceShopServiceCell *_cell = (RoadsideAssistanceShopServiceCell*)cell;
//    NSLog(@"cell.info : %@" ,_cell.info);
//    if (seleted) {
//        [seleteShop addObject:_cell.info];
//        [self priceSum:YES price:[_cell.info customObjectForkey:@"CHEAP_PRICE"]];
//        settleView.hidden = NO;
////        tableSize = _tableView.contentSize;
////        _tableView.contentSize = CGSizeMake(tableSize.width, tableSize.height + 62);
//        
//    }else{
//        [seleteShop removeObject:_cell.info];
//        [self priceSum:NO price:[_cell.info customObjectForkey:@"CHEAP_PRICE"]];
//        if (seleteShop.count == 0) {
//            _checkInteger = 0;
//            settleView.hidden = YES;
//         }
//    }
//}

- (void)priceSum:(BOOL)bol price:(NSString *)price{
     if (bol) { //+
        _checkInteger += [price doubleValue];
    }else{ //--
        _checkInteger -= [price doubleValue];
        
    }
 
    settleView.price = [RMB stringByAppendingString:[NSString stringWithFormat:@"%.2f",_checkInteger]];

}
////进入支付界面
//- (void)pushPayViewController {
//    PayViewController *pay = [[PayViewController alloc] initWithNibName:@"PayViewController" bundle:nil];
//    pay.buyShopArr = [seleteShop copy];
//    pay.countPrice = _checkInteger;
//    [self.navigationController pushViewController:pay animated:YES];
//}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{

    if(section == 1){
        
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0,_size.height)];
        view.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(5, 2, [RextExtends getScreenFrame].size.width, _size.height)];
        label.backgroundColor = [UIColor redColor];
        if (_resultDic) {
            label.text = [@"     " stringByAppendingString:[[_resultDic objectForKey:@"seller"] customObjectForkey:@"INTRODUCE"]] ;
        }
        
         label.font = [UIFont systemFontOfSize:13.0f];
        //清空背景颜色
        label.backgroundColor = [UIColor clearColor];
        //设置字体颜色为白色
        label.textColor = [UIColor blackColor];
        //文字居中显示
        label.textAlignment = NSTextAlignmentLeft;
        //自动折行设置
        label.lineBreakMode = UILineBreakModeWordWrap;
        label.numberOfLines = 0;
        [view addSubview:label];
        return view;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 88;
    }
    return 44;

 }



- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if(section == 0){
        return 0;
    }else if (section == 2){
        return 25;
    }
    return 15;
}
 

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{

    if (section == 1) {
        NSLog(@"%s",__FUNCTION__);
        _size = [Common sizeWithContent:[[_resultDic customObjectForkey:@"seller"] objectForKey:@"INTRODUCE"] AndFont:[UIFont systemFontOfSize:13.0f] andSize:[RextExtends getScreenFrame].size andLineBreak:UILineBreakModeWordWrap];
        _size.height += 15;
        return _size.height;
    }
    return 10;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
}

@end
